package com.hdl.school.utils;

public class HexUtil {
    public static byte[] hexStr2BinArr(String hexString) {
        int len = hexString.length() / 2;
        byte[] bytes = new byte[len];
        for (int i = 0; i < len; i++) {
            bytes[i] = (byte) Integer.parseInt(hexString.substring(2*i,2*(i+1)), 16);
        }
        return bytes;
    }


    /**
     * 字节数组转16进制
     * @param bytes 需要转换的byte数组
     * @return  转换后的Hex字符串
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if(hex.length() < 2){
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }


    /**
     * hex字符串转byte数组
     * @param inHex 待转换的Hex字符串
     * @return  转换后的byte数组结果
     */
    public static byte[] hexToByteArray(String inHex){
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1){
            //奇数
            hexlen++;
            result = new byte[(hexlen/2)];
            inHex="0"+inHex;
        }else {
            //偶数
            result = new byte[(hexlen/2)];
        }
        int j=0;
        for (int i = 0; i < hexlen; i+=2){
            result[j]=hexToByte(inHex.substring(i,i+2));
            j++;
        }
        return result;
    }

    public static String timeToHex(String time){
        String result="";
        if(time==null){
            return null;
        }
        String[] ts=time.split(":");
        if(ts.length!=2){
            return null;
        }
        if(Integer.parseInt(ts[0])<16){
            result+=("0"+Integer.toHexString(Integer.parseInt(ts[0]))).toUpperCase();
        }else{
            result+=Integer.toHexString(Integer.parseInt(ts[0])).toUpperCase();
        }
        if(Integer.parseInt(ts[1])<16){
            result+=("0"+Integer.toHexString(Integer.parseInt(ts[1]))).toUpperCase();
        }else{
            result+=Integer.toHexString(Integer.parseInt(ts[1])).toUpperCase();
        }
        return result;
    }


    /**
     * Hex字符串转byte
     * @param inHex 待转换的Hex字符串
     * @return  转换后的byte
     */
    public static byte hexToByte(String inHex){
        return (byte)Integer.parseInt(inHex,16);
    }


    public static String Str2SerialHex(String chars){
        StringBuffer HexStr=new StringBuffer();
        for(int i=0;i<chars.length();i++){
            String hex=Integer.toHexString((int)chars.charAt(i));
            if(hex.length()<2){
                hex+="0"+hex;
            }
            HexStr.append(hex.toUpperCase());
        }
        return HexStr.toString();
    }

    public static String reserveHex(String hexStr) {
        if (hexStr==null) {
            return null;
        }
        if(hexStr.length() % 2 != 0){
            hexStr="0"+hexStr;
        }
        StringBuffer sb = new StringBuffer();
        int length = hexStr.length() / 2;
        for (int i = length-1; i >=0; i--) {
            sb.append(hexStr.substring(i * 2, (i + 1) * 2));
        }
        return sb.toString();
    }

    public static boolean isHexStr(String hexStr) {
        if(hexStr==null||hexStr.length()==0){
            return false;
        }
        String lowerCaseStr=hexStr.toLowerCase();
        for(int i=0;i<lowerCaseStr.length();i++){
            int code=lowerCaseStr.charAt(i);
            if(!((code>=48&&code<=57)||((code>=97&&code<=102)))){
                return false;
            }
        }
        return true;
    }
}
